package com.zynn.service.module.canal.service.impl;

import com.zynn.common.core.utils.TypeConvertUtil;
import com.zynn.feign.module.es.SyncEventFeignService;
import com.zynn.message.queue.constant.MqQueueNameConstant;
import com.zynn.service.module.canal.service.SchoolInviteRecordService;
import com.zynn.service.module.canal.service.SchoolPopularityInviterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘猛
 * @since 2018-09-14
 */
@Service
@Slf4j
public class SchoolPopularityInviterServiceImpl implements SchoolPopularityInviterService {

    @Autowired
    private SyncEventFeignService syncEventFeignService;

    @JmsListener(destination = MqQueueNameConstant.SCHOOL_POPULARITY_INVITER)
    public void findDegFriend(Map<String, Object> columnsMap) {
        log.info("FIND_FRIEND_FROM_SCHOOL_POPULARITY_INVITER:{}", columnsMap);
        Long beLikeUserId = TypeConvertUtil.objectToLong(columnsMap.get("be_like_user_id"));
        if(beLikeUserId == null || beLikeUserId == 0){
            return;
        }
        /**
         * 邀请用户ID
         */
        Long inviteUserId = TypeConvertUtil.objectToLong(columnsMap.get("inviter_user_id"));
        if(inviteUserId!=null && inviteUserId!=0) {
            log.info("计算{}好友-高校版推荐", inviteUserId);
            syncEventFeignService.initFriendVersionSchool(inviteUserId,beLikeUserId);
        }

        /**
         * 被邀请用户id
         */
        Long beInviterUserId = TypeConvertUtil.objectToLong(columnsMap.get("be_inviter_user_id"));
        if(beInviterUserId!=null && beInviterUserId!=0) {
            log.info("计算{}好友-高校版推荐", beInviterUserId);
            syncEventFeignService.initFriendVersionSchool(beInviterUserId,beLikeUserId);
        }
    }


    @JmsListener(destination = MqQueueNameConstant.PROFILE_SCHOOL_POPULARITY_INVITER)
    public void findProfileFriend(Map<String, Object> columnsMap) {
        /**
         * 邀请用户ID
         */
        Long inviteUserId = TypeConvertUtil.objectToLong(columnsMap.get("inviter_user_id"));
        /**
         * 被邀请用户id
         */
        Long beInvitedUserId = TypeConvertUtil.objectToLong(columnsMap.get("be_inviter_user_id"));
        if(
                Objects.nonNull(inviteUserId) && inviteUserId > 0
             && Objects.nonNull(beInvitedUserId) && beInvitedUserId > 0) {
            syncEventFeignService.initFriendVersionProfile(inviteUserId,beInvitedUserId);
        }
    }
}
